import cv2
import numpy as np


def gasuss_noise(image, mean=0, var=0.001):
    '''
        添加高斯噪声
        mean : 均值
        var : 方差
    '''
    image = np.array(image/255, dtype=float)
    noise = np.random.normal(mean, var ** 0.5, image.shape)
    out = image + noise
    if out.min() < 0:
        low_clip = -1.
    else:
        low_clip = 0.
    out = np.clip(out, low_clip, 1.0)
    out = np.uint8(out*255)

    return out


filename = r'../week2/lena.jpg'
img = cv2.imread(filename)
img = cv2.resize(img, None, fx=0.7, fy=0.7, interpolation=cv2.INTER_CUBIC)
img_addGasussNoise = gasuss_noise(img)  # 原图像加入高斯噪声

blur = cv2.blur(img, (5,5))  # 平均滤波,内核大小为5X5
gauss = cv2.GaussianBlur(img, (5,5), 0)  # 高斯滤波
median = cv2.medianBlur(img, 5)  # 中值滤波

cv2.imshow('Image', img)
cv2.imshow('img_addGasussNoise', img_addGasussNoise)
cv2.imshow('Blurred', blur)
cv2.imshow('Gauss', gauss)
cv2.imshow('Median filtered', median)

cv2.waitKey()
cv2.destroyAllWindows()